"""
参数设置页面
"""
import streamlit as st
from models.parameter import Parameter
from core.parameter_manager import ParameterManager


def show():
    """
    显示参数设置页面
    """
    st.title("参数设置")
    
    # 初始化参数管理器
    param_manager = ParameterManager()
    
    # 参数类型选择
    param_type = st.radio(
        "参数类型",
        options=["基础参数", "业务参数", "自定义参数"],
        horizontal=True
    )
    
    # 根据类型显示不同参数表单
    if param_type == "基础参数":
        st.subheader("基础参数配置")
        with st.form("basic_params_form"):
            time_period = st.selectbox(
                "测算时间段",
                options=["月度", "季度", "年度", "自定义"]
            )
            currency = st.selectbox(
                "货币单位",
                options=["人民币(CNY)", "美元(USD)", "欧元(EUR)", "其他"]
            )
            tax_rate = st.number_input(
                "税率(%)",
                min_value=0.0,
                max_value=100.0,
                value=13.0,
                step=0.1
            )
            
            submitted = st.form_submit_button("保存基础参数")
            if submitted:
                params = [
                    Parameter("time_period", "测算时间段", time_period),
                    Parameter("currency", "货币单位", currency),
                    Parameter("tax_rate", "税率", tax_rate)
                ]
                param_manager.save_parameters(params)
                st.success("基础参数保存成功！")
    
    elif param_type == "业务参数":
        st.subheader("业务参数配置")
        with st.form("business_params_form"):
            growth_rate = st.number_input(
                "增长率(%)",
                min_value=-100.0,
                max_value=1000.0,
                value=10.0,
                step=0.1
            )
            cost_ratio = st.number_input(
                "成本占比(%)",
                min_value=0.0,
                max_value=100.0,
                value=30.0,
                step=0.1
            )
            initial_investment = st.number_input(
                "初始投资",
                min_value=0.0,
                value=1000000.0,
                step=10000.0
            )
            
            submitted = st.form_submit_button("保存业务参数")
            if submitted:
                params = [
                    Parameter("growth_rate", "增长率", growth_rate),
                    Parameter("cost_ratio", "成本占比", cost_ratio),
                    Parameter("initial_investment", "初始投资", initial_investment)
                ]
                param_manager.save_parameters(params)
                st.success("业务参数保存成功！")
    
    else:
        st.subheader("自定义参数")
        with st.form("custom_params_form"):
            param_name = st.text_input("参数名称*")
            param_label = st.text_input("显示名称")
            param_value = st.text_input("参数值*")
            
            submitted = st.form_submit_button("添加自定义参数")
            if submitted:
                if not param_name or not param_value:
                    st.error("请填写参数名称和值")
                else:
                    param = Parameter(param_name, param_label or param_name, param_value)
                    param_manager.add_custom_parameter(param)
                    st.success(f"自定义参数 '{param_name}' 添加成功！")
    
    # 显示当前参数
    st.divider()
    st.subheader("当前参数设置")
    
    params = param_manager.get_all_parameters()
    if not params:
        st.info("暂无参数设置")
    else:
        for param in params:
            col1, col2 = st.columns([1, 3])
            with col1:
                st.text(f"{param.label}:")
            with col2:
                st.text(param.value)